//
//  SQLProtocol.swift
//  Scaner
//
//  Created by liyahao on 2025/4/14.
//

import UIKit
import SQLite

protocol SQLProtocol: JSONable {
    static var table: Table { get }
    static var tableCreate: String { get }
}

extension SQLProtocol {
    // 创建数据库
    static var tableCreate: String {
        let intKeys: [String] = []
        let strKeys: [String] = []
        let optKeys: [String] = []
        let optIntKeys: [String] = []
        
        return Self.table.create(ifNotExists: true) { table in
            table.column(SQLite.Expression<Int64>("id"), primaryKey: true)
            
            intKeys.forEach { value in
                table.column(SQLite.Expression<Int>(value))
            }
            
            strKeys.forEach { value in
                table.column(SQLite.Expression<String>(value))
            }
            
            optKeys.forEach { value in
                table.column(SQLite.Expression<String?>(value))
            }
            
            optIntKeys.forEach { value in
                table.column(SQLite.Expression<Int?>(value))
            }
        }
    }
    
    // 创建数据库
    static func createDB() {
        do {
            try SQLManager.share.local.db.run(Self.tableCreate)
        } catch {
            print (error)
        }
    }
}

struct TestModel: SQLProtocol {
    static var table: Table = SQLite.Table("aa")
    
    var tableCreate: String {
        return ""
    }
    
    init() {
        TestModel.createDB()
    }
}
